//
//  InLandCityView.m
//  旅游咨询
//
//  Created by 高建 on 15/4/21.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "InLandCityView.h"

@implementation InLandCityView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)setArrOfImages:(NSMutableArray *)arrOfImages
{
    if (_arrOfImages != arrOfImages) {
        [_arrOfImages release];
        _arrOfImages = [arrOfImages retain];
    }
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width / 3, self.frame.size.height / 2, 122, 36)];
    self.pageControl.numberOfPages = arrOfImages.count;
    self.pageControl.currentPage = 0;
    
    self.scrollview = [[UIScrollView alloc] initWithFrame:self.frame];
    self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * arrOfImages.count, self.scrollview.frame.size.height);
    self.scrollview.delegate = self;
    self.scrollview.showsHorizontalScrollIndicator = NO;
    self.scrollview.showsVerticalScrollIndicator = NO;
    self.scrollview.pagingEnabled = YES;
    
    for (int i = 0; i < arrOfImages.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollview.frame.size.width, self.scrollview.frame.size.height)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image0%d", i + 1]];
//        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        [imageView setTag:i + 1];
        
        if (i != 0) {
            imageView.alpha = 0;
        }
        [self addSubview:imageView];
        [imageView release];
    }
    
    [self.pageControl addTarget:self action:@selector(pgCntlChanged:) forControlEvents:UIControlEventValueChanged];
    [self performSelector:@selector(startAnimatingScrl) withObject:nil afterDelay:3.0];
    [self addSubview:self.scrollview];
    [self addSubview:self.pageControl];
}

- (void)startAnimatingScrl
{
    if (self.arrOfImages.count) {
        [self.scrollview scrollRectToVisible:CGRectMake(((self.pageControl.currentPage + 1) % self.arrOfImages.count) *self.scrollview.frame.size.width, 0, self.scrollview.frame.size.width, self.frame.size.height) animated:YES];
        [self.pageControl setCurrentPage:((self.pageControl.currentPage + 1) % self.arrOfImages.count)];
        [self performSelector:@selector(startAnimatingScrl) withObject:nil afterDelay:3.0];
    }
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.previousTouchPoint = scrollView.contentOffset.x;
}

- (void)pgCntlChanged:(UIPageControl *)sender
{
    [self.scrollview scrollRectToVisible:CGRectMake(sender.currentPage * self.scrollview.frame.size.width, 0, self.scrollview.frame.size.width, self.scrollview.frame.size.height) animated:YES];
//    [self cancelScrollAnimation];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self.pageControl setCurrentPage:self.scrollview.bounds.origin.x / self.scrollview.frame.size.width];
//    [self cancelScrollAnimation];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.scrollview setContentOffset:CGPointMake(self.scrollview.contentOffset.x, 0)];
    int page = floor((self.scrollview.contentOffset.x - self.frame.size.width) / self.frame.size.width) + 1;
    float OldMin = self.frame.size.width * page;
    int value = self.scrollview.contentOffset.x - OldMin;
    float alpha = (value % (int) self.frame.size.width) / self.frame.size.width;
    
    if (self.previousTouchPoint > self.scrollview.contentOffset.x) {
        page = page + 2;
    } else {
        page += 1;
    }
    
    UIView *nextPage = [self.scrollview.superview viewWithTag:page + 1];
    UIView *previousPage = [self.scrollview.superview viewWithTag:page - 1];
    UIView *currentPage = [scrollView.superview viewWithTag:page];
    
    if (self.previousTouchPoint <= self.scrollview.contentOffset.x) {
        if ([currentPage isKindOfClass:[UIImageView class]]) {
            currentPage.alpha = 1 - alpha;
        }
        if ([nextPage isKindOfClass:[UIImageView class]]) {
            nextPage.alpha = alpha;
        }
        if ([previousPage isKindOfClass:[UIImageView class]]) {
            previousPage.alpha = 0;
        }
    } else if (page != 0) {
        if ([currentPage isKindOfClass:[UIImageView class]]) {
            currentPage.alpha = alpha;
        }
        if ([nextPage isKindOfClass:[UIImageView class]]) {
            nextPage.alpha = 0;
        }
        if ([previousPage isKindOfClass:[UIImageView class]]) {
            previousPage.alpha = 1- alpha;
        }
    }
    
    if (!self.didEndAnimate && self.pageControl.currentPage == 0) {
        for (UIView *imageView in self.subviews) {
            if (imageView.tag != 1 && [imageView isKindOfClass:[UIImageView class]]) {
                imageView.alpha = 0;
            } else if ([imageView isKindOfClass:[UIImageView class]]) {
                imageView.alpha = 1;
            }
        }
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
